package cn.cjxy.springmvcdemo1.controller;

import cn.cjxy.springmvcdemo1.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * SpringMVC中的Controller就是单例的(使用成员变量请注意线程安全问题)
 * 注：不要忘了,配置全注解扫描包才能认识@Controller
 * @author Administrator
 */
@Controller
public class Controller03{

	@RequestMapping("/method01") //配置访问路径
	public ModelAndView method01(){
		System.out.println(this.getClass()+":method01");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/WEB-INF/controller.jsp");
		return modelAndView;
	}
	
	@RequestMapping("/controller/method02") //配置访问路径
	public ModelAndView method012(){
		System.out.println(this.getClass()+":method02");
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/WEB-INF/controller.jsp");
		return modelAndView;
	}

    //使用HttpServletRequest接收(不建议使用)
    /**
     * http://localhost/params04?name=admin&age=24
     */
    @RequestMapping("/params04")
    public ModelAndView params04(HttpServletRequest request){
        System.out.println(request.getParameter("name"));
        System.out.println(request.getParameter("age"));
        return null;
    }

    //普通数据接收
    /** 前后端参数名称一致
     * http://localhost/params01?name=admin&age=24
     */
    @RequestMapping("/params01")
    public ModelAndView params01(String name,Integer age){
        System.out.println("name:"+name);
        System.out.println("age:"+age);
        return null;
    }

/**
 * **名称与传递参数不一致：使用**@RequestParam("xxx")**注解**
 * 如果传过来的名称与接收的名称不一致
 * http://localhost/params02?name=admin&age=24
 */
    @RequestMapping("/params02")
    public ModelAndView params02(@RequestParam("name") String username, Integer age){
        System.out.println("username:"+username);
        System.out.println("age:"+age);
        return null;
    }


    /*
     * http://localhost/params03?name=admin&age=24&date=2017-05-14
     * 	注意：接收日期必需在 set时间前配置：@DateTimeFormat(pattern="yyyy-MM-dd")
     */
    @RequestMapping("/params03")
    public ModelAndView params03(User user){
        System.out.println("name:"+user.getName());
        System.out.println("age:"+user.getAge());
        System.out.println("date:"+user.getDate());
        return null;
    }

    /**
     * 接收url中参数的请求,接收用户请求参数值
     * http://localhost/params05/delete/100/tom
     * 	注：这种方式为RESTful风格，参数不易过多
     */
    @RequestMapping("/params05/delete/{id}/{username}")
    public ModelAndView params05(@PathVariable("id")Long id,@PathVariable("username")String name){
        System.out.println(id);
        System.out.println(name);
        return null;
    }
    //视图解析器
    @RequestMapping("/method03")
    public ModelAndView method03() {
        System.out.println(this.getClass() + ":method03");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("a");
        return modelAndView;
    }
    //转发
    @RequestMapping("/data05")
    public String data5(Model model, HttpSession session){
        model.addAttribute("test","张三");//可以取出数据
        session.setAttribute("testsession", "张三session");//可以取出数据
        return "forward:/data.jsp";
    }
    //重定向
    @RequestMapping("/data06")
    public String data6(Model model, HttpSession session){
        model.addAttribute("test","张三");//可以取出数据
        session.setAttribute("testsession", "张三session");//可以取出数据
        return "redirect:/data.jsp";
    }
    @RequestMapping("/datajson")
    //返回json格式
    @ResponseBody//指定返回的类型为json  且会跳过视图解析器
    public User testjson(){
        User user = new User();
        user.setAge(23);
        user.setName("张三");
        return user;

    }

    }